package com.it.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.enity.pageResult;
import com.it.enity.result;
import com.it.goods.TbBrand;
import com.it.mapper.Brandmapper;
import com.it.service.goods.brandservice;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @author zhangyan
 * @version 1.0
 * @date 2019/7/8 19:20
 */
@Service
public class brandserviceimpl implements brandservice {

    @Autowired
    private Brandmapper brandmapper;


    @Override
    public List<TbBrand> findAll() {
        return brandmapper.selectAll();

    }

    @Override
    public pageResult<TbBrand> findbypage(int startpage, int size) {
        PageHelper.startPage(startpage, size);
        Page<TbBrand> result = (Page<TbBrand>) brandmapper.selectAll();
        return new pageResult<TbBrand>(result.getTotal(), result.getResult());
    }

    @Override
    public List<TbBrand> findlist(Map<String, Object> map) {
        Example example = createExample(map);
        return brandmapper.selectByExample(example);
    }

    @Override
    public pageResult findpage(Map<String, Object> map, int startpage, int size) {
        PageHelper.startPage(startpage, size);
        Example example = createExample(map);
        Page<TbBrand> result = (Page<TbBrand>) brandmapper.selectByExample(example);
        return new pageResult<TbBrand>(result.getTotal(), result.getResult());
    }

    @Override
    public TbBrand findById(Integer id) {
        return brandmapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(TbBrand brand) {
        brandmapper.insertSelective(brand);
    }

    @Override
    public void update(TbBrand brand) {
        brandmapper.updateByPrimaryKeySelective(brand);
    }

    @Override
    public void delete(Integer id) {
        brandmapper.deleteByPrimaryKey(id);
    }


    private Example createExample(Map<String, Object> map) {
        Example example = new Example(TbBrand.class);
        Example.Criteria criteria = example.createCriteria();
        if (map != null) {
            if (map.get("name") != null && !"".equals(map.get("name"))) {
                criteria.andLike("name", "%" + (String) map.get("name") + "%");
            }
            if (map.get("letter") != null && !"".equals(map.get("letter"))) {
                criteria.andEqualTo("letter", map.get("letter"));
            }
        }
        return example;
    }


}
